package com.example.jay.day45.adapter;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.example.jay.day45.R;

import java.util.List;

/**
 *
 */
public class BlueAdapter extends BaseAdapter {

    private List<BluetoothDevice> devices;

    private LayoutInflater inflater;

    public BlueAdapter(List<BluetoothDevice> devices, Context context) {
        this.devices = devices;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return devices.size();
    }

    @Override
    public Object getItem(int position) {
        return devices.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_bluetooth_layout, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.name.setText(devices.get(position).getName());
        holder.address.setText(devices.get(position).getAddress());
        return convertView;
    }

    private static class ViewHolder {
        private TextView name;
        private TextView address;


        public ViewHolder(View itemView) {
            name = (TextView) itemView.findViewById(R.id.item_name);
            address = (TextView) itemView.findViewById(R.id.item_address);
        }
    }

}
